#region License Information
// Copyright (c) 2005 - 2009, Allan Spartacus Mangune (allan@owlpal.com)
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, 
// are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this list 
//   of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice, this list of 
//   conditions and the following disclaimer in the documentation and/or other materials provided 
//   with the distribution.
//
// * Neither the name of Owlpal nor the names of its contributors may be used to endorse or 
//   promote products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS 
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 
// BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Web.Security;

using Owlpal.Models;

namespace Owlpal.Controllers
{
    public class NewsletterController : Controller
    {

        public ActionResult List()
        {
            List<Newsletter> newsletters = new NewsletterManager().FindAllNewsletters();
            return View(newsletters);
        }

        public ActionResult Show(Guid id)
        {
            Newsletter newsletter = new Newsletter().Find(id);
            return View(newsletter);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [Authorize(Roles="Administrators")]
        public ActionResult Index()
        {
            List<Newsletter> newsletters = new NewsletterManager().FindAllNewsletters();
            return View(newsletters);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Details(Guid id)
        {
            Newsletter newsletter = new Newsletter().Find(id);
            return View(newsletter);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [Authorize(Roles = "Administrators")]
        public ActionResult Create()
        {
            return View();
        } 

        /// <summary>
        /// 
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        [Authorize(Roles = "Administrators")]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection collection)
        {
            Newsletter newsletter = new Newsletter();
            SetNewsletterAttributes(collection, newsletter);
            if (ModelState.IsValid)
            {
                newsletter.Create();
                return RedirectToAction("Index");
            }
            else
            {
                return View();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="newsletter"></param>
        private void SetNewsletterAttributes(FormCollection collection, Newsletter newsletter)
        {
            string status = (string)collection["Status"];
            string body = (string)collection["Body"];
            string subject = (string)collection["Subject"];
            if (string.IsNullOrEmpty(status))
            {
                ModelState.AddModelError("Status", "You must specify the Status.");
            }
            else
            {
                newsletter.Status = status;
            }
            if (string.IsNullOrEmpty(body))
            {
                ModelState.AddModelError("Body", "You must specify the Body.");
            }
            else
            {
                newsletter.Body = body;
            }
            if (string.IsNullOrEmpty(subject))
            {
                ModelState.AddModelError("Body", "You must specify the Subject.");
            }
            else
            {
                newsletter.Subject = subject;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Authorize(Roles = "Administrators")]
        public ActionResult Edit(Guid id)
        {
            Newsletter newsletter = new Newsletter().Find(id);
            return View(newsletter);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="collection"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        [Authorize(Roles = "Administrators")]
        [ValidateInput(false)]
        public ActionResult Edit(Guid id, FormCollection collection)
        {
            Newsletter newsletter = new Newsletter();
            newsletter.ID = id;
            if (id != null)
            {
                if (Guid.Empty != id)
                {
                    SetNewsletterAttributes(collection, newsletter);
                    if (ModelState.IsValid)
                    {
                        newsletter.Update();
                        return RedirectToAction("Show", new { id = newsletter.ID });
                    }
                    else
                    {
                        return View("Edit",newsletter);
                    }
                }
                else
                {
                    ShowErroneousNewsletterID(ref id);
                }
            }
            else
            {
                ShowErroneousNewsletterID(ref id);
            }
            return View(newsletter);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Get)]
        [Authorize(Roles = "Administrators")]
        public ActionResult Delete(Guid id)
        {
            if (id != null)
            {
                if (id != Guid.Empty)
                {
                    Newsletter newsletter = new Newsletter();
                    newsletter.ID = id;
                    newsletter.Delete();
                    return RedirectToAction("Index");
                }
                else
                {
                    return ShowErroneousNewsletterID(ref id);
                }
            }
            else
            {
                return ShowErroneousNewsletterID(ref id);
            }
        }

        private ActionResult ShowErroneousNewsletterID(ref Guid id)
        {
            ModelState.AddModelError("_FORM", "The Newsletter ID is missing or in a wrong format");
            return View("Delete", new { id = id });
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        [Authorize(Roles = "Administrators")]
        public ActionResult SendNewsletter(Guid id, FormCollection collection)
        {
            if (id != null)
            {
                if (Guid.Empty != id)
                {
                    NewsletterManager newsletterManager = new NewsletterManager();
                    newsletterManager.SendNewsletter(new Newsletter().Find(id));
                    return RedirectToAction("Details", new { id = id });
                }
                else
                {
                    return ShowErroneousNewsletterID(ref id);
                }
            }
            else
            {
                return ShowErroneousNewsletterID(ref id);
            }
        }
    }
}
